package com.zizy.healthexpert.Database;



import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private static final String DB_NAME = "health_expert.db"; // 数据库名称
	private static final int version = 9; // 数据库版本
    private DatabaseHelper myDBHelper;
    private Context context;

	private SQLiteDatabase db;

    public DatabaseHelper(Context context){

        super(context, DB_NAME, null, version);
        this.context=context;

    }

	@Override
	//The method  called when the database  first created so when the database is exist it won't be called any more
	public void onCreate(SQLiteDatabase db) {
		
	}
	public void deleteTable(String tableName){
		Log.i("Log in Drop Table", "Done");
		db.execSQL("DROP TABLE " +tableName + ";"); // 刪除舊有的資料表
	}
	public void createTable(String tableName){
 		String sql = "create table if not exists "+ tableName +" (placeName varchar(20) not null ,placeType varchar(20) not null, placeLocation_Latitude real(60) not null,placeLocation_Longitude real(60) not null );";
		this.db.execSQL(sql);
	}
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //db.execSQL("DROP TABLE " +tableName + ";"); // 刪除舊有的資料表
            Log.i("Log in update",  "OK");
            onCreate(db);

        }

    public DatabaseHelper open() throws SQLException {
        myDBHelper = new DatabaseHelper(context);
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    //judge if the database is exist, if not , return false
    static public boolean isDatabaseExist(){
        try {
            boolean exists=new File("/data/data/com.zizy.healthexpert/databases/"+DB_NAME).exists();
            return exists;
        } catch (Exception e) {
            return false;
        }
    }

    //copy database from the folder assets  into default database folder
    public void createDatabase() throws IOException {

            InputStream assetsDB = context.getAssets().open("health_expert.db3");
            if (!new File ("/data/data/com.zizy.healthexpert/databases/").exists()){
                new File ("/data/data/com.zizy.healthexpert/databases/").mkdir();
            }
            if (!new File ("/data/data/com.zizy.healthexpert/databases/"+DB_NAME).exists()){
                new File ("/data/data/com.zizy.healthexpert/databases/"+DB_NAME).createNewFile();
            }

            OutputStream dbOut = new FileOutputStream( new File ("/data/data/com.zizy.healthexpert/databases/"+DB_NAME));
             byte[] buffer = new byte[1024];
            int length;
            while ((length = assetsDB.read(buffer))>0){
                dbOut.write(buffer, 0, length);
            }

            dbOut.flush();
            dbOut.close();
            assetsDB.close();


    }
}
